<template>
  <view class="container">
    <!-- 类型切换 -->
    <view class="tab-section">
      <view 
        class="tab-item" 
        v-for="(item, index) in tabList" 
        :key="index"
        :class="{ active: currentTab === item.value }"
        @tap="switchTab(item.value)"
      >
        <text>{{item.label}}</text>
      </view>
    </view>
    
    <!-- 优惠券列表 -->
    <view class="coupon-list">
      <view 
        class="coupon-item" 
        v-for="(item, index) in couponList" 
        :key="index"
        :class="{ disabled: item.status !== 'unused' }"
      >
        <view class="left">
          <view class="amount">
            <text class="symbol">¥</text>
            <text class="number">{{item.amount}}</text>
          </view>
          <view class="condition">满{{item.minAmount}}元可用</view>
        </view>
        <view class="right">
          <view class="info">
            <text class="name">{{item.name}}</text>
            <text class="time">{{item.startTime}} - {{item.endTime}}</text>
            <text class="desc">{{item.description}}</text>
          </view>
          <view class="action">
            <text class="status" v-if="item.status !== 'unused'">{{item.statusText}}</text>
            <button 
              class="get-btn" 
              v-else
              @tap="getCoupon(item)"
            >立即领取</button>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 空状态 -->
    <view class="empty" v-if="couponList.length === 0">
      <image src="/static/images/empty-coupon.png" mode="aspectFit"></image>
      <text>暂无优惠券</text>
    </view>
    
    <!-- 使用说明 -->
    <view class="rules-section">
      <view class="title">使用说明</view>
      <view class="rules-list">
        <view class="rule-item" v-for="(item, index) in rules" :key="index">
          <text class="dot"></text>
          <text>{{item}}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { couponApi } from '@/api/index'
import { showToast } from '@/utils/common'

export default {
  data() {
    return {
      tabList: [
        { label: '未使用', value: 'unused' },
        { label: '已使用', value: 'used' },
        { label: '已过期', value: 'expired' }
      ],
      currentTab: 'unused',
      couponList: [],
      rules: [
        '优惠券仅限在有效期内使用',
        '每个订单只能使用一张优惠券',
        '优惠券不可叠加使用',
        '优惠券不可与其他优惠活动同时使用',
        '优惠券不可兑换现金',
        '优惠券不可转让'
      ]
    }
  },
  onLoad() {
    this.getCouponList()
  },
  methods: {
    // 切换类型
    switchTab(value) {
      this.currentTab = value
      this.getCouponList()
    },
    
    // 获取优惠券列表
    async getCouponList() {
      try {
        const res = await couponApi.getCouponList({
          status: this.currentTab
        })
        this.couponList = res
      } catch (error) {
        showToast('优惠券列表加载失败')
      }
    },
    
    // 领取优惠券
    async getCoupon(coupon) {
      try {
        await couponApi.getCoupon(coupon.id)
        showToast('领取成功')
        this.getCouponList()
      } catch (error) {
        showToast('领取失败')
      }
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
}

.tab-section {
  display: flex;
  background: #fff;
  padding: 20rpx 0;
  position: sticky;
  top: 0;
  z-index: 1;
  
  .tab-item {
    flex: 1;
    text-align: center;
    position: relative;
    
    text {
      font-size: 28rpx;
      color: #666;
    }
    
    &.active {
      text {
        color: #ff0000;
        font-weight: bold;
      }
      
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        bottom: -20rpx;
        transform: translateX(-50%);
        width: 40rpx;
        height: 4rpx;
        background: #ff0000;
        border-radius: 2rpx;
      }
    }
  }
}

.coupon-list {
  padding: 20rpx;
  
  .coupon-item {
    display: flex;
    margin-bottom: 20rpx;
    background: #fff;
    border-radius: 12rpx;
    overflow: hidden;
    
    &.disabled {
      opacity: 0.6;
      
      .left {
        background: #999;
      }
    }
    
    .left {
      width: 200rpx;
      background: #ff0000;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      padding: 20rpx;
      
      .amount {
        color: #fff;
        
        .symbol {
          font-size: 32rpx;
        }
        
        .number {
          font-size: 60rpx;
          font-weight: bold;
          margin-left: 4rpx;
        }
      }
      
      .condition {
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.8);
        margin-top: 10rpx;
      }
    }
    
    .right {
      flex: 1;
      padding: 20rpx;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      
      .info {
        .name {
          font-size: 32rpx;
          color: #333;
          margin-bottom: 10rpx;
          display: block;
        }
        
        .time {
          font-size: 24rpx;
          color: #999;
          margin-bottom: 10rpx;
          display: block;
        }
        
        .desc {
          font-size: 24rpx;
          color: #666;
          display: block;
        }
      }
      
      .action {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        
        .status {
          font-size: 24rpx;
          color: #999;
        }
        
        .get-btn {
          width: 160rpx;
          height: 60rpx;
          line-height: 60rpx;
          text-align: center;
          background: #ff0000;
          color: #fff;
          border-radius: 30rpx;
          font-size: 24rpx;
        }
      }
    }
  }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 200rpx;
  
  image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
  }
  
  text {
    font-size: 28rpx;
    color: #999;
  }
}

.rules-section {
  background: #fff;
  padding: 30rpx;
  margin-top: 20rpx;
  
  .title {
    font-size: 28rpx;
    color: #333;
    font-weight: bold;
    margin-bottom: 20rpx;
  }
  
  .rules-list {
    .rule-item {
      display: flex;
      align-items: flex-start;
      margin-bottom: 10rpx;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .dot {
        width: 8rpx;
        height: 8rpx;
        background: #999;
        border-radius: 50%;
        margin-top: 16rpx;
        margin-right: 10rpx;
      }
      
      text {
        font-size: 24rpx;
        color: #666;
        flex: 1;
      }
    }
  }
}
</style> 